#include "../include/server.h"
int add_mysql_usr(MYSQL *mysql_con, char *name, char *passwd, int *usr_id)
{
    Usr user;
    memset(&user, 0, sizeof(Usr));
    char salt[13] = {0};
    strcpy(salt, GenerateStr());
    strcpy(user.username, name);
    strcpy(user.salt, salt);
    strcpy(user.cryptpasswd, crypt(passwd, salt));
    sprintf(user.pwd, "/%s/", name);
    char sql_cmd[2 * MAXSTR] = {0};
    sprintf(sql_cmd, "insert into TUSR(username,salt,cryptpasswd,pwd) values('%s','%s','%s','%s');", user.username, user.salt, user.cryptpasswd, user.pwd);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("add_mysql_usr error:%s\n", mysql_error(mysql_con));
        return F_ret;
    }
    printf("add_mysql_usr success\n");
    select_mysql_usr(mysql_con, name, passwd, usr_id);
    return S_ret;
}
int select_mysql_usr(MYSQL *mysql_con, char *name, char *passwd, int *usr_id)
{
    Usr user;
    memset(&user, 0, sizeof(Usr));
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "select id,salt,cryptpasswd from TUSR where username='%s';", name);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("add_mysql_usr error:%s\n", mysql_error(mysql_con));
        return F_ret;
    }
    MYSQL_RES *mysql_res;
    MYSQL_ROW mysql_row; //行数据
    mysql_res = mysql_store_result(mysql_con);
    while ((mysql_row = mysql_fetch_row(mysql_res)) != NULL)
    {
        if (strcmp(crypt(passwd, mysql_row[1]), mysql_row[2]) == 0)
        {
            printf("select strcmp==0\n");
            *usr_id = atoi(mysql_row[0]);
            return S_ret;
        }
    }
    return F_ret;
}
char *GenerateStr()
{
    char *str = calloc(1, sizeof(char) * (STR_LEN + 1));
    int i, flag;
    srand(time(NULL));
    for (i = 0; i < STR_LEN; i++)
    {
        flag = rand() % 3;
        switch (flag)
        {
        case 0:
            str[i] = rand() % 26 + 'a';
            break;
        case 1:
            str[i] = rand() % 26 + 'A';
            break;
        case 2:
            str[i] = rand() % 10 + '0';
            break;
        }
    }
    printf("Generate str=%s strlen(str)=%ld\n", str, strlen(str));
    return str;
}
int usr_fat_init(MYSQL *mysql_con, int usr_id, char *name)
{
    Fat_File fat_file;
    memset(&fat_file, 0, sizeof(fat_file));
    strcpy(fat_file.filename, name);
    fat_file.owner_id = usr_id;
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "insert into TFILE(filename,owner_id) values('%s',%d);", fat_file.filename, fat_file.owner_id);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("add_mysql_usr error:%s\n", mysql_error(mysql_con));
        return F_ret;
    }
    printf("add_mysql_usr success\n");
    return S_ret;
}
void del_mysql_usr(MYSQL *mysql_con, int usr_id)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "delete from TUSR where id=%d", usr_id);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("delete error:%s\n", mysql_error(mysql_con));
    }
    printf("delete success\n");
}
void del_file(Task *task, char *filename)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "delete from TFILE where owner_id=%d and parent_id=%d and filename='%s' and type=0;", task->usr.usr_id, task->usr.dir_id, filename);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("delete error:%s\n", mysql_error(task->mysql_con));
    }
    printf("delete success\n");
}
void select_dir_id(MYSQL *mysql_con, Login_Usr *login_usr)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "select id from TFILE where parent_id=0 and owner_id=%d and type=1;", login_usr->usr_id);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("select error:%s\n", mysql_error(mysql_con));
    }
    MYSQL_RES *mysql_res;
    MYSQL_ROW mysql_row; //行数据
    mysql_res = mysql_store_result(mysql_con);
    while ((mysql_row = mysql_fetch_row(mysql_res)) != NULL)
    {
        login_usr->dir_id = atoi(mysql_row[0]);
    }
}
MYSQL_RES *select_ls_cd_sql(Task *task)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "select filename,type,id from TFILE where owner_id=%d and parent_id=%d;", task->usr.usr_id, task->usr.dir_id);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("select error:%s\n", mysql_error(task->mysql_con));
    }
    return mysql_store_result(task->mysql_con);
}
MYSQL_RES *do_pwd_sql(MYSQL *mysql_con, int cur_id)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "select filename,parent_id from TFILE where id=%d;", cur_id);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("select error:%s\n", mysql_error(mysql_con));
    }
    return mysql_store_result(mysql_con);
}
int add_file_sql(Task *task, Fat_File *newfile)
{
    char sql_cmd[2 * MAXSTR] = {0};
    sprintf(sql_cmd, "insert into TFILE(parent_id,filename,owner_id,md5,filesize,type) values(%d,'%s',%d,'%s',%d,%d);", newfile->parent_id, newfile->filename, newfile->owner_id, newfile->md5, newfile->filesize, newfile->type);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("add_mysql_usr error:%s\n", mysql_error(task->mysql_con));
        return S_ret;
    }
    printf("add_mysql_usr success\n");
    return F_ret;
}
int selectfile_through_md5_sql(MYSQL *mysql_con, char *md5_str)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "select filesize from TFILE where md5='%s';", md5_str);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("select error:%s\n", mysql_error(mysql_con));
    }
    MYSQL_RES *mysql_res;
    MYSQL_ROW mysql_row; //行数据
    mysql_res = mysql_store_result(mysql_con);
    if ((mysql_row = mysql_fetch_row(mysql_res)) != NULL)
    {
        return atoi(mysql_row[0]);
    }
    else
    {
        return -1;
    }
}
void modify_file_info_sql(Task *task, char *filename, char *md5_str, int filesize)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "update TFILE set md5='%s',filesize=%d where parent_id=%d and filename='%s' and owner_id=%d and type=0;", md5_str, filesize, task->usr.dir_id, filename, task->usr.usr_id);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("update error:%s\n", mysql_error(task->mysql_con));
    }
}
void insert_file_sql(MYSQL *mysql_con, Fat_File *fat_file)
{
    char sql_cmd[2 * MAXSTR] = {0};
    sprintf(sql_cmd, "insert into TFILE(parent_id,filename,owner_id,md5,filesize,type) values(%d,'%s',%d,'%s',%d,%d);", fat_file->parent_id, fat_file->filename, fat_file->owner_id, fat_file->md5, fat_file->filesize, fat_file->type);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("insert_file_sql error:%s\n", mysql_error(mysql_con));
    }
    printf("insert file sql success\n");
}
int select_file_sql(Task *task, char *filename, int *exist, char *md5_str)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "select md5,filesize from TFILE where owner_id=%d and parent_id=%d and filename='%s' and type=0;", task->usr.usr_id, task->usr.dir_id, filename);
    printf("sql_cmd=%s\n", sql_cmd);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("select error:%s\n", mysql_error(task->mysql_con));
    }
    MYSQL_RES *mysql_res;
    MYSQL_ROW mysql_row; //行数据
    mysql_res = mysql_store_result(task->mysql_con);
    if ((mysql_row = mysql_fetch_row(mysql_res)) != NULL)
    {
        strcpy(md5_str, mysql_row[0]);
        *exist = 1;
        printf("md5_str=%s\n", md5_str);
        return atoi(mysql_row[1]);
    }
}
void insert_md5_sql(Task *task, char *md5)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "insert into TMD5(md5) values('%s');", md5);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("insert_tmd5_sql error:%s\n", mysql_error(task->mysql_con));
    }
    printf("insert tmd5 sql success\n");
}
void modify_md5_sql(Task *task, char *md5)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "update TMD5 set usenum=usenum+1 where md5='%s';", md5);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("modify_tmd5_sql error:%s\n", mysql_error(task->mysql_con));
    }
    printf("modify tmd5 sql success\n");
}
void modify_tmd5_md5(MYSQL *mysql_con, char *oldmd5, char *newmd5)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "update TMD5 set md5='%s' where md5='%s';", newmd5, oldmd5);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("modify_tmd5_md5 error:%s\n", mysql_error(mysql_con));
    }
    printf("modify tmd5 md5 success\n");
}
void sub_md5_sql(MYSQL *mysql_con, char *md5)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "update TMD5 set usenum=usenum-1 where md5='%s';", md5);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("sub_md5_sql\n");
        printf("modify_tmd5_sql error:%s\n", mysql_error(mysql_con));
    }
    printf("modify tmd5 sql success\n");
}
int select_md5_num(Task *task, char *md5)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "select usenum from TMD5 where md5='%s';", md5);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("select_md5_num error\n");
    }
    MYSQL_RES *mysql_res;
    MYSQL_ROW mysql_row; //行数据
    mysql_res = mysql_store_result(task->mysql_con);
    if ((mysql_row = mysql_fetch_row(mysql_res)) != NULL)
    {
        ret = atoi(mysql_row[0]);
    }
    return ret;
}
void del_md5_sql(MYSQL *mysql_con, char *md5)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "delete from TMD5 where md5='%s';", md5);
    int ret = mysql_real_query(mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("delete error:%s\n", mysql_error(mysql_con));
    }
    printf("delete success\n");
}
int select_md5_file(Task *task, char *filename, char *md5)
{
    char sql_cmd[MAXSTR] = {0};
    sprintf(sql_cmd, "select md5 from TFILE where filename='%s' and owner_id=%d and parent_id=%d and type=0;", filename, task->usr.usr_id, task->usr.dir_id);
    int ret = mysql_real_query(task->mysql_con, sql_cmd, strlen(sql_cmd));
    if (ret != 0)
    {
        printf("select_md5_file error:%s\n", mysql_error(task->mysql_con));
        return F_ret;
    }
    MYSQL_RES *mysql_res;
    MYSQL_ROW mysql_row; //行数据
    mysql_res = mysql_store_result(task->mysql_con);
    if ((mysql_row = mysql_fetch_row(mysql_res)) != NULL)
    {
        strcpy(md5, mysql_row[0]);
    }
    else
    {
        return F_ret;
    }
    return S_ret;
}
